package hos.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.management.RuntimeErrorException;

import hos.po.Staff;
import hos.util.JDBCUtil;
public class StaffDaoImp implements StaffDao{

	@Override
	public int insertStaff(Staff staff) {
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			String sql = "insert into staff values(?,?,?,?,?,?,?,?)";
			con = JDBCUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, staff.getS_id());
			pstmt.setString(2, staff.getS_name());
			pstmt.setString(3, staff.getS_gender());
			pstmt.setString(4, staff.getS_tel());
			pstmt.setInt(5, staff.getS_age());
			pstmt.setString(6, staff.getS_job());
			pstmt.setString(7, staff.getDepart_id());
			pstmt.setString(8, staff.getPwd());
			int res = pstmt.executeUpdate();
			return res;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			JDBCUtil.close(pstmt, con);
		}
		return 0;
	}

	@Override
	public Staff selectByIdAndPwd(String id,String pwd) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select * from staff where s_id = ? and password = ?";
		try {
			con = JDBCUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, id);
			pstmt.setString(2,pwd);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				return new Staff();
			}else {
				return null;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.close(pstmt, con, rs);
		}
		return null;
	}

	@Override
	public Staff[] selectByDepartId(String depart_id) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = JDBCUtil.getConnection();
			String sql = "select * from staff where depart_id =?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, depart_id);
			rs = pstmt.executeQuery();
			//将所有查询到的职工信息封装到数组中
			Staff[] s_Return = new Staff[50];
			int lenth = 0;
			while(rs.next()) {
				Staff s_re = new Staff();
				s_re.setDepart_id(rs.getString("depart_id"));
				s_re.setPwd(rs.getString("password"));
				s_re.setS_age(rs.getInt("s_age"));
				s_re.setS_gender(rs.getString("s_gender"));
				s_re.setS_id(rs.getString("s_id"));
				s_re.setS_job(rs.getString("s_job"));
				s_re.setS_name(rs.getString("s_name"));
				s_re.setS_tel(rs.getString("s_tel"));
				s_Return[lenth] = s_re;
				lenth++;
			}
			if(lenth > 0) {
				return s_Return;
			}else {
				return null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("查询全部职工信息出错");
		}finally {
			JDBCUtil.close(pstmt, rs, con);
		}
		return null;
	}


}
